package com.fjec.market.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.SpecialMapUtil;
import com.fjec.market.service.IBizRatingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 013.商品评价表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-09-22
 */
@RestController
@RequestMapping("/")
public class BizRatingController {

    @Resource
    private IBizRatingService service;


    /**
     * 订单评价
     *
     * @param id              订单Id
     * @param qualityRating   品质
     * @param speedRating     发货速度
     * @param logisticsRating 物流配送
     * @param skuComment      描述
     * @return
     */
    @PostMapping("/y/phy/order/sub/rating")
    public JsonResult subOrderRating(Long id, Integer qualityRating, Integer speedRating, Integer logisticsRating,
                                     @RequestParam(required = false) String skuComment,
                                     @RequestParam(required = false) String picIds,
                                     @RequestParam(required = false) String picUrls) {
        return service.subOrderRating(id, qualityRating, speedRating, logisticsRating, skuComment, picIds, picUrls);
    }

    @GetMapping("/n/rating/getPageBySpuId")
    public JsonResult getPageBySpuId(Long spuId, Page page) {
        List<Map<String, Object>> ratings = service.getPageBySpuId(spuId, page);
        List<Map<String, Object>> result = new ArrayList<>();
        ratings.forEach(rating -> result.add(SpecialMapUtil.lowKey(rating)));
        Long total = page.getTotal();
        return JsonResult.success().data(result).total(total).build();
    }
}
